﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;

public partial class Admin_Product : System.Web.UI.Page
{
    DataDB dataDB = new DataDB();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!this.Page.IsPostBack)
        {
            this.BindDDLProduct();
            this.BindDDLCategory();
        }
    }
    
    protected void btnKaydet_Click(object sender, EventArgs e)
    {
        string file = "";

        if (ViewState["UrunID"] != null)
            dataDB.UrunGuncelle(Convert.ToInt32(ViewState["UrunID"]), Convert.ToInt32(rblServiceProduct.SelectedValue),
                                Convert.ToInt32(ddlCategory.SelectedValue), txtName.Text, txtProductOverView.Text,
                                txtProductDetail.Text,
                                nbPrice.Amount, file);
        else
        {
            ViewState["UrunID"] = dataDB.UrunEkle(Convert.ToInt32(rblServiceProduct.SelectedValue),
                                Convert.ToInt32(ddlCategory.SelectedValue), txtName.Text, txtProductOverView.Text,
                                txtProductDetail.Text,
                                nbPrice.Amount, file);
        }
    }
    private void BindDDLProduct()
    {
        Util.Bindings.BindDdl(dataDB.UrunListesiGetir(-1, Convert.ToInt32(rblServiceProduct.SelectedValue)), ddlProduct, "ProductName","ID");
    }
    private void BindDDLCategory()
    {
        Util.Bindings.BindDdl(dataDB.KategoriListesiGetir(Convert.ToInt32(rblServiceProduct.SelectedValue)), ddlCategory, "CategoryName", "ID");
    }
    protected void rblServiceProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDDLCategory();
    }
    private void Fill()
    {
        if (ViewState["UrunID"] != null)
        {
            DataRow urunDr = dataDB.UrunGetir(Convert.ToInt32(ViewState["UrunID"]));
            if(urunDr != null)
            {
                rblServiceProduct.SelectedValue = urunDr["TypeID"].ToString();

                txtName.Text = urunDr["ProductName"].ToString();
                txtProductOverView.Text = urunDr["ProductOverview"].ToString();
                txtProductDetail.Text = urunDr["ProductDetail"].ToString();

                nbPrice.Amount = Convert.ToDecimal(urunDr["Price"].ToString());
                ddlCategory.SelectedValue = urunDr["CategoryID"].ToString();
            }
        }
    }
    protected void btnGetir_Click(object sender, EventArgs e)
    {
        ViewState["UrunID"] = ddlProduct.SelectedValue;
        Fill();
    }
    protected void btnSil_Click(object sender, EventArgs e)
    {
        ViewState["UrunID"] = null;
        Util.Common.ClearControl(pnl1);
    }
}